/*
 * Lab3-PartII Corrected.pro - Corrected version of the aless and lookup predicates.
 * 2008 Ron Gosa
 * CSC430 Mike Quals
 */


/* Aless predicate rules.
   The aless function is used by lookup to determine which branch of the tree is looked up
   recursively. */

aless(X, Y) :- 
    str_atom(L, X),
    str_atom(M, Y),
    alessx(L, M).

alessx([], [_|_]).

alessx([X|_], [Y|_]) :- 
    X < Y.
    
alessx([P|Q], [R|S]) :-
    P = R,
    alessx(Q, S).


/* Lookup predicate rules.
   The lookup funcitn returns the number of wins of a supplied name. If the name does not
   exist in the structure than the informaiton is added. */

lookup(H, w(H, G, _, _), G) :- !.

lookup(H, w(H1, _, Before, _), G) :-
    aless(H, H1),
    lookup(H, Before, G).
    
lookup(H, w(H1, _, _, After), G) :-
    not(aless(H, H1)),
    lookup(H, After, G).
    